package com.zmn.plat.services.impl.channel;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.plat.model.entity.channel.ChannelCityPriceRatio;
import com.zmn.plat.model.entity.channel.ChannelCityPriceRatioQuery;
import com.zmn.plat.model.vo.channel.ChannelCityPriceRatioVo;
import com.zmn.plat.persistence.interfaces.channel.ChannelCityPriceRatioDAO;
import com.zmn.plat.services.interfaces.channel.ChannelCityPriceRatioService;

import lombok.extern.slf4j.Slf4j;

/**
 * 类描述:城市价格系数管理实现类
 *
 * @author linfeng
 * @since 2019/08/27/11:20
 */
@Service
@Slf4j
public class ChannelCityPriceRatioServiceImpl implements ChannelCityPriceRatioService {

    @Resource
    private ChannelCityPriceRatioDAO channelCityPriceRatioDAO;

    @Override
    public List<ChannelCityPriceRatioVo> listPageByQuery(ChannelCityPriceRatioQuery query) {
        int count = channelCityPriceRatioDAO.countByQuery(query);
        query.setTotalCount(count);
        return channelCityPriceRatioDAO.listPageByQuery(query);
    }

    @Override
    public ChannelCityPriceRatio findByKey(Integer ratioId) {
        return channelCityPriceRatioDAO.findByKey(ratioId);
    }

    @Override
    @Cacheable(cacheNames = "redis1h", key = "'base:ratio:findByChannelIdAndCityAndCategAndType:ChannelCityPriceRatioQuery:'+#p0", unless = "#result == null")
    public ChannelCityPriceRatio findByChannelIdAndCityAndCategAndType(ChannelCityPriceRatioQuery query) {
        if (query.getRatioType() == null ||  query.getChannelId() == null || query.getPriceType() == null) {
            log.error("必填参数不能为空");
            return null;
        }

        List<ChannelCityPriceRatio> channelCityPriceRatios = channelCityPriceRatioDAO.listByQuery(query);
        if (CollectionUtil.isNotNullOrEmpty(channelCityPriceRatios)) {
            return channelCityPriceRatios.get(0);
        }

        return null;
    }

    @Override
    public List<ChannelCityPriceRatio> listByQuery(ChannelCityPriceRatioQuery query) {
        return channelCityPriceRatioDAO.listByQuery(query);
    }

    @Override
    public List<ChannelCityPriceRatio> listAll() {
        return channelCityPriceRatioDAO.listAll();
    }

    @Override
    public Integer countByQuery(ChannelCityPriceRatioQuery query) {
        return channelCityPriceRatioDAO.countByQuery(query);
    }

    @Override
    public Integer updateByKey(ChannelCityPriceRatio channelCityPriceRatio) {
        return channelCityPriceRatioDAO.updateByKey(channelCityPriceRatio);
    }

    @Override
    public void updateToBeEffectiveRatioBatch(List<ChannelCityPriceRatio> updateRatioList) {
        channelCityPriceRatioDAO.updateToBeEffectiveRatioBatch(updateRatioList);
    }

    @Override
    public Integer updateRatioByKeyBatch(List<ChannelCityPriceRatio> ratioList) {
        return channelCityPriceRatioDAO.updateRatioByKeyBatch(ratioList);
    }

    @Override
    public Integer insert(ChannelCityPriceRatio channelCityPriceRatio) {
        return channelCityPriceRatioDAO.insert(channelCityPriceRatio);
    }

    @Override
    public Integer insertBatch(List<ChannelCityPriceRatio> channelCityRatioList) {
        return channelCityPriceRatioDAO.insertBatch(channelCityRatioList);
    }

    @Override
    public Integer copyRatio(Integer sourceChannelId, Integer targetChannelId, Integer bizType, Integer priceType, Date effectiveDate, List<Integer> ratioTypes, String opertor) {
        return channelCityPriceRatioDAO.copyRatio(sourceChannelId, targetChannelId, bizType, priceType, effectiveDate, ratioTypes, opertor);
    }

    @Override
    public Integer deleteByKey(Integer ratioId) {
        return channelCityPriceRatioDAO.deleteByKey(ratioId);
    }

    @Override
    public Integer deleteByKeys(List<Integer> ratioIds) {
        return channelCityPriceRatioDAO.deleteByKeys(ratioIds);
    }

    @Override
    public void deleteByIdList(List<Integer> deleteIdList) {
        channelCityPriceRatioDAO.deleteByIdList(deleteIdList);
    }

    @Override
    public Integer deleteByChannelIdAndPriceTypeAndBizTypeAndRatioTypes(Integer channelId, Integer priceType, Integer bizType, List<Integer> ratioTypes) {
        return channelCityPriceRatioDAO.deleteByChannelIdAndPriceTypeAndBizTypeAndRatioTypes(channelId, priceType, bizType, ratioTypes);
    }

    @Override
    public void copy(Integer originalChannelId, int targetChannelId, String operator) {

        channelCityPriceRatioDAO.copy(originalChannelId, targetChannelId, operator);
    }
}
